package com.adk.backend.controller;

import com.adk.backend.annotation.RequiresPermission;
import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxDeptList;
import com.adk.backend.service.DeptListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门列表管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/dept-list")
public class DeptListController {
    
    @Autowired
    private DeptListService deptListService;
    
    /**
     * 分页查询部门列表
     */
    @RequiresPermission("list")
    @GetMapping("/list")
    public Result<PageResult<LxDeptList>> getDeptList(
            @RequestParam(required = false) String deptName,
            @RequestParam(required = false) Integer companyId,
            @RequestParam(required = false) Long parentId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("deptName", deptName);
        params.put("companyId", companyId);
        params.put("parentId", parentId);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        PageResult<LxDeptList> result = deptListService.getDeptList(params);
        return Result.success(result);
    }
    
    /**
     * 获取所有部门（不分页）
     */
    @RequiresPermission("view")
    @GetMapping("/all")
    public Result<List<LxDeptList>> getAllDeptList(@RequestParam(required = false) Integer companyId) {
        List<LxDeptList> result = deptListService.getAllDeptList(companyId);
        return Result.success(result);
    }
    
    /**
     * 根据父级ID获取子部门列表
     */
    @RequiresPermission("view")
    @GetMapping("/by-parent")
    public Result<List<LxDeptList>> getDeptListByParentId(@RequestParam Long parentId) {
        List<LxDeptList> result = deptListService.getDeptListByParentId(parentId);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询部门
     */
    @RequiresPermission("view")
    @GetMapping("/detail")
    public Result<LxDeptList> getDeptDetail(@RequestParam Long id) {
        LxDeptList dept = deptListService.getDeptById(id);
        if (dept != null) {
            return Result.success(dept);
        }
        return Result.error("部门不存在");
    }
    
    /**
     * 创建部门
     */
    @RequiresPermission("add")
    @PostMapping("/create")
    public Result<LxDeptList> createDept(@RequestBody LxDeptList deptList) {
        try {
            LxDeptList result = deptListService.createDept(deptList);
            return Result.success("创建成功", result);
        } catch (Exception e) {
            log.error("创建部门失败", e);
            return Result.error("创建部门失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新部门
     */
    @RequiresPermission("edit")
    @PostMapping("/update")
    public Result<Boolean> updateDept(@RequestBody LxDeptList deptList) {
        try {
            boolean success = deptListService.updateDept(deptList);
            if (success) {
                return Result.success("更新成功", true);
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新部门失败", e);
            return Result.error("更新部门失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除部门
     */
    @RequiresPermission("del")
    @PostMapping("/delete")
    public Result<Boolean> deleteDept(@RequestParam Long id) {
        try {
            boolean success = deptListService.deleteDept(id);
            if (success) {
                return Result.success("删除成功", true);
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除部门失败", e);
            return Result.error("删除部门失败：" + e.getMessage());
        }
    }
}

